﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

namespace TheHeGeoCommercial.Master
{
    public partial class UcHMenu : System.Web.UI.UserControl
    {
        protected const string SESSION_H_MENU = "hmenu";
        protected void Page_Load(object sender, EventArgs e)
        {
             ASPxGridView1.Selection.UnselectAll();
            if (!IsPostBack)
            {
                LoadMenu();
            }
            else
            {
                ASPxGridView1.DataSource = Session[SESSION_H_MENU];
                ASPxGridView1.DataBind();
            }
            if (Request.RawUrl.Contains("Products"))
            {
                if (Request.QueryString["type"] != null)
                {
                    string str = Request.QueryString["type"];
                    SetSubHeader(SelectMenu(str));
                }
                else
                {
                    SetSubHeader("Tất cả tất cả thiết bị");
                }
               
            }
            else if (Request.RawUrl.Contains("ProductDetail"))
            {
                if (Request.QueryString["id"] != null)
                {
                    using (var ent = new Dao.TheheGeoCommercialEntities())
                    {
                        string str = Request.QueryString["id"];
                        var dt = ent.SanPhams.FirstOrDefault(p => p.MaSanPham == str);
                        if (dt != null)
                        {
                            SelectMenu(dt.MaLoaiSanPham);
                            SetSubHeader(dt.TenSanPham);
                        }
                    }
                }
                else
                {
                    Response.Redirect("Products.aspx");
                }
                
            }
        }

        protected string SelectMenu(string type)
        {
            var dt = (DataTable)Session[SESSION_H_MENU];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["MaLoaiSanPham"].ToString()==(type))
                {
                    ASPxGridView1.Selection.SelectRow(i);
                    return dt.Rows[i]["TenLoaiSanPham"].ToString();
                }
            }
            return "";
        }

        protected void SetSubHeader(string txt)
        {
            var page = Page.Master as ComMaster;
            if (page != null)
            {
                var barMasterPage = page;
                barMasterPage.SetLblSubHeaderTxt(txt);
            }
        }

        protected void LoadMenu()
        {
            try
            {
                using (var ent = new Dao.TheheGeoCommercialEntities())
                {
                    var dt = new DataTable();
                    dt.Columns.Add("TenLoaiSanPham");
                    dt.Columns.Add("MaLoaiSanPham");
                    var ls = (from d in ent.LoaiSanPhams select new {d.MaLoaiSanPham, d.TenLoaiSanPham});
                    foreach (var l in ls)
                    {
                        var dr = dt.NewRow();
                        dr["TenLoaiSanPham"] = l.TenLoaiSanPham;
                        dr["MaLoaiSanPham"] = l.MaLoaiSanPham;
                        dt.Rows.Add(dr);
                    }
                    Session[SESSION_H_MENU] = dt;
                    ASPxGridView1.DataSource = dt;
                    ASPxGridView1.DataBind();
                }
            }
            catch (Exception)
            {
            }

        }
    }
}